package model

import "gorm.io/gorm/clause"

type Export struct {
	Id       string 	`xml:"-" json:"id" gorm:"column:id;primary_key;not null;index"`
	Type     string 	`xml:"-" json:"type"`
	Host     string		`xml:"-" json:"host"`
	Port     *int		`xml:"-" json:"port"`
	Url      string		`xml:"-" json:"url"`
	User     string		`xml:"-" json:"user"`
	Passwd   string		`xml:"-" json:"passwd"`
	Dev_type string		`xml:"-" json:"dev_type"`
}
func FindExports(filter *Export)[]Export{
	var exports []Export
	if err:=sqldb.Find(&exports,filter).Error;err != nil{
		return nil
	}else{
		return exports
	}
}
func DeleteExports()(int64,error){
	var exports []Export
	sqldb.Find(&exports)
	index :=0
	for i:=0; i < len(exports); i++{
		if _,err := exports[i].Delete();err != nil{
			return int64(index),err
		}
		index += 1
	}
	return int64(index),nil
}
func (p* Export) Insert()error{
	return sqldb.Create(p).Error
}
func (p* Export) Save()(int64,error){
	ret := sqldb.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "id"}},
		DoUpdates: clause.AssignmentColumns([]string{"type", "host","port","url","user","passwd","dev_type"}),
	}).Updates(p)
	return ret.RowsAffected,ret.Error
}
func (p* Export) Delete()(int64,error){
	db :=sqldb.Where(&Export{Id:p.Id}).Delete(&Export{})
	return db.RowsAffected,db.Error
}